package com.hnchances.studentscoresystem.exception;

import com.hnchances.studentscoresystem.dto.response.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public CommonResponse<Void> handleBusinessException(BusinessException e) {
        log.error("业务异常：{}", e.getMsg(), e);
        return CommonResponse.fail(e.getCode(), e.getMsg());
    }

    /**
     * 处理系统异常
     */
    @ExceptionHandler(Exception.class)
    public CommonResponse<Void> handleSystemException(Exception e) {
        log.error("系统异常：", e);
        return CommonResponse.fail(ErrorCode.SYSTEM_ERROR.getCode(), ErrorCode.SYSTEM_ERROR.getMsg());
    }
}